/*-
 * Copyright (c) 2006, 2007 FTS Team
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 4. Neither the name of the FTS Team nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *      Passage.hpp
 *
 * $FTS$
 */
#ifndef _PASSAGE_HPP__
#define _PASSAGE_HPP__ 1

/**
  @file Passage.hpp
  @brief Passage of words
*/

#include <FTS.hpp>
#include <Pair.hpp>
#include <STLVector.hpp>

namespace FTSS // FullTextSearchSystem
{

#define C_WORD_PROCESSED       0x10000000

#define C_WORD_HAS_ALT_WORDS   0x00000001
#define C_WORD_DOES_NOT_EXIST  0x00000010

/**
  @struct WordEntryPos Passage.hpp <Passage.hpp>
  @brief Position of base point
*/
struct WordEntryPos
{
	/** Word Entry                       */
	WordEntry   word_entry;
	/** Position in query                */
	UINT_16     query_pos;
	/** Equal positions                  */
	UINT_16     query_eq_pos;
	/** Additional flags                 */
	UINT_32     flags;
	/** WordForm                         */
	UINT_16     orig_form;
	/** Base point Rank                  */
	W_FLOAT     okapi_rank;
	/** Base point Rank for nearest pair */
	W_FLOAT     okapi_pair_rank;
	/** Term IDF                         */
	W_FLOAT     idf;
	/**
	  @brief Default constructor
	*/
	inline WordEntryPos(): query_pos(WordEntryPos::npos),
	                       query_eq_pos(WordEntryPos::npos),
	                       flags(0),
	                       orig_form(0),
	                       okapi_rank(0.0),
	                       okapi_pair_rank(0.0),
	                       idf(0.0) { ;; }

	/**
	  @brief Constructor
	  @param oWE - Word Entry
	  @param iPos - Position in query
	  @param iEQPos - position of right base point with equal position in document
	  @param iFlags - Additional flags
	  @param iOrigForm - original form of word
	  @param dIDF - word IDF
	*/
	inline WordEntryPos(const WordEntry  & oWE,
	                    const UINT_16    & iPos,
	                    const UINT_16    & iEQPos,
	                    const UINT_32    & iFlags,
	                    const UINT_32    & iOrigForm,
	                    const W_FLOAT    & dIDF): word_entry(oWE),
	                                              query_pos(iPos),
	                                              query_eq_pos(iEQPos),
	                                              flags(iFlags),
	                                              orig_form(iOrigForm),
	                                              okapi_rank(0.0),
	                                              okapi_pair_rank(0.0),
	                                              idf(dIDF) { ;; }

	/** No position */
	static const UINT_16 npos;
};

/**
  @struct Passage Passage.hpp <Passage.hpp>
  @brief Passage of words
*/
struct Passage
{
private:
	/**
	  @brief Default constructor
	*/
	Passage();
public:
	/** No position */
	static const UINT_32 npos;

	UINT_32                     max_pos;
	/** Number of positions in query */
	UINT_32                     positions;
	/** Passage base point           */
	WordEntry                   basepoint;
	/** Passage weight               */
	W_FLOAT                     rank;
	/** Cumulative IDF of passage    */
	W_FLOAT                     idf;
	/** Min. position in base points array */
	UINT_32                     min_bp_pos;
	/** Max position in base points array  */
	UINT_32                     max_bp_pos;
	/** Positions in base points vector    */
	UINT_32                   * base_points_pos;
	/** Ranks of words in passage    */
	W_FLOAT                   * word_ranks;

	/**
	  @brief Constructor
	  @param iPositions - max. passage size
	  @brief Default constructor
	  @param iPositions - number or words in passage
	*/
	inline Passage(const UINT_32 & iPositions): max_pos(iPositions), positions(0), rank(0.0), idf(0.0), min_bp_pos(Passage::npos), max_bp_pos(Passage::npos)
	{
		base_points_pos = new UINT_32[iPositions];
		word_ranks      = new W_FLOAT[iPositions];
		for (UINT_32 iI = 0; iI < iPositions; ++iI)
		{
			base_points_pos[iI] = Passage::npos;
			word_ranks[iI]      = 0.0;
		}
	}

	/**
	  @brief A destructor
	*/
	inline ~Passage() throw()
	{
		delete [] base_points_pos;
		delete [] word_ranks;
	}

	/**
	  @brief Copy constructor
	  @param oRhs - object to copy
	*/
	Passage(const Passage & oRhs);

	/**
	  @brief Copy operator
	  @param oRhs - object to copy
	  @return reference to self
	*/
	Passage & operator =(const Passage & oRhs);
};

} // namespace FTSS
#endif // _PASSAGE_HPP__
// End.
